#include "stdafx.h"

#include "Diamond.h"

namespace ETM
{
	Diamond::Diamond()
	{
		Diamond(0, 0);
	}

	Diamond::Diamond(float x, float y)
	{
		timer = 0.0f;
		sprite.SetPosition(x, y);
		collected = false;
	}

	void Diamond::Load()
	{
		sprite.SetImage(*IM.GetResource("Images/diamond.png"));
		anim = Animation(5, 0.1f, 32, 0, 32, 32);
	}

	void Diamond::Update(Player& player, float delta)
	{
		timer += delta;

		anim.Update(delta);

		sf::Vector2f dPos = Utils::SpriteCenter(sprite);
		sf::Vector2f pPos = Utils::SpriteCenter(player.GetSprite());
		float dist = abs(sqrt(pow(dPos.x - pPos.x, 2) + pow(dPos.y - pPos.y, 2)));

		if(dist < 32.0f)
			OnCollected(player);
	}

	void Diamond::OnCollected(ETM::Player &player)
	{
		collected = true;
	}

	void Diamond::Render(sf::RenderWindow& window)
	{
		sprite.SetSubRect(anim.GetRect());
		window.Draw(sprite);
	}
}